package xyz.vmcserver.wdibbs.model.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xyz.vmcserver.wdibbs.model.po.ArticleView;
import xyz.vmcserver.wdibbs.model.service.ArticleService;
import xyz.vmcserver.wdibbs.model.service.ToolsService;
import xyz.vmcserver.wdibbs.model.vo.Article;
import xyz.vmcserver.wdibbs.model.vo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/allZone")
    public String allZone(){
        return "user/allZone";
    }

    @RequestMapping("/createArticle.do")
    public String createArticle(HttpServletRequest httpServletRequest, HttpSession session) throws IOException {
        User user = (User)session.getAttribute("isLoginUser");
        String title =  httpServletRequest.getParameter("title");
        String content =  httpServletRequest.getParameter("newArticle");
        String code = httpServletRequest.getParameter("articleUniqueCode");
        Integer permission = Integer.valueOf(httpServletRequest.getParameter("articlePermission"));

        java.util.Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(day));

        Article article = new Article(null, user.getUsername(), title,code+".html","STU001",permission,sdf.format(day),user.getUsername(),sdf.format(day),0,0,code);
        ToolsService toolsService = new ToolsService();
        toolsService.pageGeneration(httpServletRequest,article,code,content);
        article.setCreateTime(null);
        article.setRedactTime(null);

        articleService.postArticle(article);
//        httpServletRequest.setAttribute("articleCode","/article/"+code+".html");
        return "redirect:/article/"+code+".html";
    }

    @RequestMapping("/home")
    public ModelAndView home(@RequestParam(name = "page",defaultValue = "1") Integer page){
        ModelAndView modelAndView = new ModelAndView();
        List<ArticleView> articleViews = articleService.latestArticles((page-1)*15);
        modelAndView.addObject("latestArticles",articleViews);
        modelAndView.setViewName("user/homepage");
        return modelAndView;
    }

    @RequestMapping("/createArticle")
    private String createArticle1(HttpServletRequest httpServletRequest){
        User user;
        if(null==httpServletRequest.getSession().getAttribute("isLoginUser")){
            user = new User(-1,null,"游客","游客", 0, null);
        }else{
            user = (User)httpServletRequest.getSession().getAttribute("isLoginUser");
        }
        System.out.println("************username:"+user.getUsername());
        return "user/createArticle";
    }


    @RequestMapping("/getArticleNumber")
    @ResponseBody
    public Integer getArticleNumber(){
        Integer num = articleService.queryArticleNumber();
        if(null!=num){
            return num;
        }else{
            return 0;
        }
    }

}
